package cn.edu.neusoft.yuejian.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.List;

import cn.edu.neusoft.yuejian.R;
import cn.edu.neusoft.yuejian.activity.HdInfoActivity;
import cn.edu.neusoft.yuejian.bean.activity;
import cn.edu.neusoft.yuejian.bean.contest;


/**
 * Created by Administrator on 2017/9/25.
 */

public class bsadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<contest> mDataList;
    private LayoutInflater mLayoutInflater;
    public bsadapter(Context mContext, List<contest> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_bs, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final contest entity = (contest) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.bs_name.setText(entity.getName());
        //viewHolder.bs_scs.setText(entity.getAttend_num());

        Picasso.with(mContext)
                .load("http://172.24.10.175/workout/Uploads/"+entity.getThumb())
                .transform(new CropSquareTransformation())
                .error(R.drawable.wu)
                .into(viewHolder.bs_pic);
        viewHolder.bs_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,HdInfoActivity.class);

                intent.putExtra("activity_name",entity.getName());
                intent.putExtra("id",entity.getId());
//                intent.putExtra("start_date",)

//                String a=entity.getShop_id();
//                Log.d("ywfio","efbh"+a);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {

        return mDataList.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView bs_name;
        ImageView bs_pic;
        //TextView hd_scs;
        public ViewHolder(View itemView) {
            super(itemView);
            bs_name = (TextView) itemView.findViewById(R.id.bs_name);
            bs_pic = (ImageView) itemView.findViewById(R.id.bs_pic);
            //hd_scs = (TextView) itemView.findViewById(R.id.hd_scs);
        }
    }
    public class CropSquareTransformation implements Transformation {
        @Override public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
            Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
            if (result != source) {
                source.recycle();
            }
            return result;
        }

        @Override public String key() { return "square()"; }
    }
}
